
// 返回一个随机数
function rand(min,max){
    return parseInt(Math.random()*(max-min+1))+min;
}

// 返回一个随机的颜色16进制
function color(){
    var str = "#";
    for(var i=0;i<6;i++){
        str += rand(0,15).toString(16);
    }
    return str;
}

// 返回指定dom节点的attr样式值
function getStyle(dom,attr){
    if(window.getComputedStyle){
        // 说明有getComputedStyle方法
        return window.getComputedStyle(dom,null)[attr]
    }else{
        return dom.currentStyle[attr]
    }
}

// 单属性缓动
function move(dom,attr,target,callback){
    clearInterval(dom.timer);
    dom.timer = setInterval(function(){
        if(attr == "opacity"){
            var current = parseInt(getStyle(dom,attr)*100);
        }else{
            var current = parseInt(getStyle(dom,attr));
        }
        var speed = target-current>0?Math.ceil((target-current)/10):Math.floor((target-current)/10);
        var next = current+speed;
        if(attr=='zIndex'){
            var next = target;
            dom.style.zIndex = target;
        }else if(attr=="opacity"){
            dom.style.opacity = next/100;
            dom.style.filter = "alpha(opacity="+next+")";
        }else{
            dom.style[attr] = next+"px";
        }
        if(next==target){
            clearInterval(dom.timer);
            typeof callback==='function'&&callback()
        }
    },20)
}

//多属性同时缓动
function animate(dom,json,callback){
    clearInterval(dom.timer)
    dom.timer = setInterval(function(){
        var flag = true;
        for(var attr in json){
            if(attr=="opacity"){
                var current = parseInt(getStyle(dom,attr)*100);
            }else{
                var current = parseInt(getStyle(dom,attr));
            }
            var speed = json[attr]-current>0?Math.ceil((json[attr]-current)/10):Math.floor((json[attr]-current)/10)
            if(attr=="zIndex"){
                var next = json[attr];
            }else{
                var next = current + speed;
            }
            if(attr=="zIndex"){
                dom.style[attr] = next;
            }else if(attr=="opacity"){
                dom.style.opacity = next/100;
                dom.style.filter = "alpha(opacity="+next+")";
            }else{
                dom.style[attr] = next+"px";
            }
            if(next!=json[attr]){
                flag = false;
            }
        }

        if(flag){
            clearInterval(dom.timer);
            typeof callback==='function'&&callback()
        }
    },20)
}

//返回页面滚动的距离
function scroll(){
    return {
        left:document.body.scrollLeft||document.documentElement.scrollLeft||window.pageXOffset,
        top:document.body.scrollTop||document.documentElement.scrollTop||window.pageYOffset
    }
}

//返回节点距离页面的距离
function getDistance(dom){
    var distance={
        left:0,
        top:0
    }
    while(true){
        distance.left=distance.left+dom.offsetLeft;
        distance.top=distance.top+dom.offsetTop;
        dom=dom.offsetParent;
        if(dom.nodeName=="BODY"){
            return distance;
        }
    }
}

     
function trim(str){
    var reg=/^\s+|\s+$/;
    return str.replace(reg,'')
}
